package de.duester.vectordrawing.object;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint.Align;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Typeface;
import de.duester.vectordrawing.GraphicalObject;

/**
 * @author Maxim Duester
 */
public class Text extends GraphicalObject {
	private String text;
	private String font;
	private int size = 11;
	private boolean bold;
	private boolean italic;
	private boolean underline;

	/**
	 * 
	 */
	public Text() {
	}

	/**
	 * @param p
	 * @param text
	 */
	public Text(PointF p, String text) {
		super(p);
		this.text = text;
	}

	/**
	 * @param font
	 */
	public void setFont(String font) {
		this.font = font;
	}

	/**
	 * @return
	 */
	public String getFont() {
		return font;
	}

	/**
	 * @param size
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * @return
	 */
	public int getSize() {
		return size;
	}

	/**
	 * @param bold
	 */
	public void setBold(boolean bold) {
		this.bold = bold;
	}

	/**
	 * @return
	 */
	public boolean isBold() {
		return bold;
	}

	/**
	 * @param italic
	 */
	public void setItalic(boolean italic) {
		this.italic = italic;
	}

	/**
	 * @return
	 */
	public boolean isItalic() {
		return italic;
	}

	/**
	 * @param underline
	 */
	public void setUnderline(boolean underline) {
		this.underline = underline;
	}

	/**
	 * @return
	 */
	public boolean isUnderline() {
		return underline;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.GraphicalObject#getNonRotatedImage()
	 */
	@Override
	protected Bitmap getNonRotatedImage() {
		paint.setColor(getColor(0));
		paint.setTextAlign(Align.LEFT);
		Typeface t;
		if (bold && italic)
			t = Typeface.create(font, Typeface.BOLD_ITALIC);
		else if (bold)
			t = Typeface.create(font, Typeface.BOLD);
		else if (italic)
			t = Typeface.create(font, Typeface.ITALIC);
		else
			t = Typeface.create(font, Typeface.NORMAL);
		paint.setTypeface(t);
		paint.setUnderlineText(underline);
		paint.setTextSize(size);

		Rect r = new Rect();
		paint.getTextBounds(text, 0, text.length(), r);
		PointF p = getScaledAndTransformedRelative(new PointF(r.width(), r.height()));
		float w = p.x;
		float h = p.y;

		Bitmap b = Bitmap.createBitmap((int) w, (int) h, Config.ARGB_8888);
		Canvas canvas = new Canvas(b);
		canvas.drawText(text, 0, 0, paint);
		return b;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.GraphicalObject#copy()
	 */
	@Override
	public GraphicalObject copy() {
		Text t = (Text) super.copy();
		t.text = text;
		t.font = font;
		t.size = size;
		t.bold = bold;
		t.italic = italic;
		t.underline = underline;
		return t;
	}

	@Override
	public boolean equals(Object o) {
		if (!super.equals(o))
			return false;
		Text t = (Text) o;
		return (text.equals(t.text) && font.equals(t.font) && size == t.size && bold == t.bold
				&& italic == t.italic && underline == t.underline);
	}
}
